<template>
	<view class="mainpadding mainbgc">
		<!-- 展示地址 -->
		<view class="ffffff mainpadding radius mainpadding" v-if="dataAll.addressData!=null" @click="dizhi">
			<view class="flexbetween">
				<view class="">
					<view class="flexleft">
						<view class="titletext margin_right3">{{dataAll.addressData.name}}</view>
						<view class="titletext">{{dataAll.addressData.mobile}}</view>
					</view>
					<view class="margin_top2 huisetext">
						{{dataAll.addressData.cityinfo+dataAll.addressData.address_detail}}
					</view>
				</view>
				<view class="right_icon"></view>
			</view>
		</view>
		<!-- 添加地址 -->
		<view class="biabox flexcenter ffffff radius" v-if="dataAll.addressData==null" @click="dizhi">
			<view class="textcenter dhtext">点击添加收货地址</view>
		</view>
		<view v-for="item in dataAll.orderData.lists" :key="item.shop_id">
			<view class="ffffff mainpadding radius margin_top" v-for="ite in item.products" :key="ite.id">
				<view class="flexbetween">
					<view class="shilitu">
						<image :src="ite.image_text" mode=""></image>
					</view>
					<view class="" style="width: 74%;">
						<view class="ershib yhxiank">{{ite.title}}</view>
						<view class="huisetext yhxiank">{{ite.sku.difference}}</view>
						<view class="flexbetween margin_top1">
							<view class="qianshu">¥{{ite.sku.price}}
							</view>
							<view class="shuliang">*{{ite.number}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="ffffff mainpadding radius margin_top">
				<view class="flexbetween margin_top">
					<view class="strongtext">商品金额</view>
					<view class="qianshu">¥{{dataAll.orderData.statis.allsub}}</view>
				</view>
				
				<view class="flexbetween margin_top">
					<view class="strongtext">是否开具发票</view>
					<u-switch v-model="is_kp" activeColor="#228DFF"></u-switch>
				</view>
				<view class="flexbetween margin_top" v-if="is_kp" @click="bill">
					<view class="strongtext">开具发票</view>
					<view class="right_icon"></view>
				</view>
			</view>
			<view class="margin_top mainpadding ffffff radius">
				<input type="text" placeholder="请输入您的留言内容" v-model="item.remark">
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding">
			<view class="flexbetween">
				<view class="flexleft">
					<view class="titletext">合计：</view>
					<view class="daqiantext">¥{{dataAll.orderData.statis.allsub}}</view>
				</view>
				<view class="xiaohuangbtn" @click="pay">立即支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				is_jfdk: false, //是否积分抵扣
				is_kp: false, //是否开票
				lastmsg: {
					type: '',
					data: [],
				},
				addressid: "",
				dataAll: {},
				kaipiao:{
					gsmc:"",
					sh:"",
					zh:"",
					khh:"",
					address:"",
					mobile:""
				}
			}
		},
		onLoad(options) {
			this.lastmsg = JSON.parse(decodeURIComponent(options.msg));
		},
		onShow() {
			uni.$off('addressId')
			uni.$on('addressId', data => {
				this.addressid = data
			})
			uni.$off('kaipiaoevent')
			uni.$on('kaipiaoevent', data => {
				this.kaipiao = data
			})
			this.getdingdan()
		},
		methods: {
			getdingdan() {
				let data = {
					address_id: this.addressid, //地址ID
					type: this.lastmsg.type, //来源类型detail详情，car购物车
					data: this.lastmsg.data //数据列表
				}
				httpRequest.request("/api/order/getOrderGoodsList", 'POST', data, false, false, true).then(res => {
					if (res.code == 1) {
						res.data.orderData.lists.forEach(item => {
							this.$set(item, "remark", '')
							item.products.forEach(ite=>{
								ite.sku.difference = ite.sku.difference.join("*")
							})
						})
						this.dataAll = res.data
						if (res.data.addressData) {
							this.addressid = res.data.addressData.id
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			dizhi() {
				uni.navigateTo({
					url: '/pages/shopping/shouhuodizhi?type=1'
				})
			},
			bill() {
				uni.navigateTo({
					url: '/pages/shopping/tianxiefapiao'
				})
			},
			pay() {
				if (this.addressid == "") {
					httpRequest.toast("请选择地址")
					return
				}
				uni.showLoading({
					title: '请稍候...'
				})
				if (this.lastmsg.type == "cart") {
					this.delcar()
				}
				let lists = []
				this.dataAll.orderData.lists.forEach(item => {
					let arr = []
					item.products.forEach(ite => {
						arr.push({
							goods_id: ite.id,
							number: ite.number,
							sku_id: ite.sku.id
						})
					})
					lists.push({
						shop_id: item.shop_id,
						remarks: item.remark,
						products: arr
					})
				})
				let data = {
					address_id: this.addressid, //地址ID
					kp_status:this.is_kp?1:0,
					gsmc:this.kaipiao.gsmc,
					sh:this.kaipiao.sh,
					zh:this.kaipiao.zh,
					khh:this.kaipiao.khh,
					address:this.kaipiao.address,
					mobile:this.kaipiao.mobile,
					lists: lists
				}
				httpRequest.request("/api/order/addOrder", 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						let obj = {
							order_pay_no: res.data.order_pay_no,
							pay_price: res.data.pay_price,
						}
						uni.redirectTo({
							url: '/pages/shopping/querenqzf?msg=' + encodeURIComponent(JSON
								.stringify(obj))
						})
					} else {
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					uni.hideLoading()
					httpRequest.toast(err.msg);
				})
			},
			delcar() {
				let arr = this.lastmsg.data
				arr.forEach(item => {
					delete item.number
				})
				httpRequest.request('/api/cart/delCart', 'POST', {
					data: arr,
				}).then(res => {
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.daqiantext {
		font-size: 36rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD463E;
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.qianshu {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD463E;
	}

	.shuliang {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #999999;
	}

	.shilitu {
		width: 134rpx;
		height: 128rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.biabox {
		height: 168rpx;
	}

	.dhtext {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #228DFF;
	}

	.huisetext {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.yhxiank {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.gudingdb {
		padding: 30rpx 30rpx 60rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.xiaohuangbtn {
		font-size: 32rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FFFFFF;
		width: 226rpx;
		height: 76rpx;
		border-radius: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #228DFF;
	}
</style>
